import UIKit
import SnapKit

class ImageCardView: UIView {
    let imageView = UIImageView()
    let resultIcon = UIImageView()
    let resultBGView = UIView()
    private var oriItem:WordItem!
    private var selectedItem:WordItem?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    required init?(coder: NSCoder) { super.init(coder: coder); setup() }

    private func setup() {
        backgroundColor = UIColor(white: 0.08, alpha: 1)
        layer.cornerRadius = 12
        layer.borderWidth = 2
        layer.borderColor = UIColor.clear.cgColor
        clipsToBounds = true

        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        addSubview(imageView)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        //结果
        resultBGView.backgroundColor = .black.withAlphaComponent(0.5)
        resultBGView.isHidden = true
        resultBGView.circular(12)
        addSubview(resultBGView)
        resultBGView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        resultIcon.contentMode = .scaleAspectFit
        resultBGView.addSubview(resultIcon)
        resultIcon.translatesAutoresizingMaskIntoConstraints = false
        resultIcon.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width:70, height: 70))
            make.center.equalToSuperview()
        }
        
        //
        let playBtn = UIButton(type: .system)
        playBtn.setImage(UIImage(named: "播放语音"), for: .normal)
        playBtn.tintColor = .white
        playBtn.imageView?.contentMode = .scaleAspectFit
        playBtn.backgroundColor = UIColor(white: 0, alpha: 0.3)
        playBtn.layer.cornerRadius = 12
        playBtn.addTarget(self, action: #selector(onPlay), for: .touchUpInside)
        addSubview(playBtn)
        playBtn.translatesAutoresizingMaskIntoConstraints = false
        playBtn.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.top.equalToSuperview()
            make.width.equalTo(24)
            make.height.equalTo(24)
        }

    }

    func setHighlighted(_ yes: Bool) {
        layer.borderColor = yes ? UIColor.systemBlue.cgColor : UIColor.clear.cgColor
        layer.shadowColor = yes ? UIColor.systemBlue.cgColor : UIColor.black.cgColor
        layer.shadowOpacity = yes ? 0.25 : 0.12
        layer.shadowRadius = yes ? 8 : 4
        layer.shadowOffset = CGSize(width: 0, height: 3)
    }

    public func update(item:WordItem){
        self.oriItem = item
    }
    
    func onSelectedItem(_ selectedItem:WordItem?) {
        
        self.selectedItem = selectedItem
        if selectedItem == nil{
            resultBGView.isHidden = true
        }
    }
    
    //获取是否选择正确
    public func getIsCorrect()->Bool?{
        guard let item = selectedItem else {
            return nil
        }
        return item.word == oriItem.word
    }

    func reloadCheck(index:Int) {
        if let selectedItem = selectedItem{
            resultBGView.isHidden = false
            resultBGView.alpha = 0
            
            let correct = selectedItem.word == oriItem.word
            resultIcon.image = correct ? UIImage(named: "正确") : UIImage(named: "错误")
            resultIcon.tintColor = correct ? .systemGreen : .systemRed
            
            let delay = TimeInterval(index) * 0.50
            DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
                if correct{
//                    Utils.playSuccessSound()
                }else{
//                    Utils.playErrorSound()
                }
                UIView.animate(withDuration: 0.5, animations: {[weak self]in
                    guard let `self` = self else { return }
                    self.resultBGView.alpha = 1
                    self.resultBGView.transform = .identity
                })
            }
           
        }else{
            resultBGView.isHidden = true
        }
        
    }

    @objc private func onPlay() {
        guard let item  = oriItem else{return}
        Utils.play(word: item.word)
    }
}
